﻿using Newtonsoft.Json;
using PMIS.Common;
using PMIS.Controls;
using PMIS.Dialogs;
using PMIS.HttpRequest;
using PMIS.Shared.DTO;
using PMIS.Shared.Models;
using PMIS.Views.Menu;
using PMIS.Views.Role;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace PMIS.ViewModels.Role
{
    public class RoleViewModel : ViewModelBase
    {
        private readonly IDialogService _dialogService;
        private readonly HttpService _httpService;

        public RoleViewModel(IDialogService dialogService, HttpService httpService)
        {
            _dialogService = dialogService;
            _httpService = httpService;
            RoleList = new ObservableCollection<RoleDto>();
            LoadRoleList();
        }

        private async void LoadRoleList()
        {
            var response = await _httpService.GetAsync(Api.ROLE_GET_LIST);
            if (response?.code == 1)
            {
                var data = JsonConvert.DeserializeObject<PagingModel<RoleDto>>(JsonConvert.SerializeObject(response.data));
                var items = data.Items.OrderBy(p => p.Sort);
                RoleList = new ObservableCollection<RoleDto>(items);
            }
        }

        private ObservableCollection<RoleDto>? _roleList;
        public ObservableCollection<RoleDto>? RoleList
        {
            get
            {
                return _roleList;
            }
            set
            {
                SetProperty(ref _roleList, value);
            }
        }

        private void OnAdd()
        {
            _dialogService.ShowDialog(nameof(AddEditRole), null, AddEditCallBack);
        }

        private async void OnDelete()
        {
            var count = RoleList.Count(p => p.IsChecked == true);
            if (count == 0)
            {
                ToastManager.ShowToast("请选择要删除的数据");
                return;
            }
            var dialogResult = System.Windows.MessageBox.Show("是否删除选择的数据", "提示", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (dialogResult == MessageBoxResult.Yes)
            {
                List<string?> ids = RoleList.Where(p => p.IsChecked == true).Select(p => p.Id).ToList();
                await DeleteRoleList(ids);
                ids.ForEach((id) =>
                {
                    var r = RoleList.First(p => p.Id == id);
                    RoleList.Remove(r);
                });
            }
        }

        private async Task<bool> DeleteRoleList(List<string?> ids)
        {
            var response = await _httpService.PostAsync(Api.ROLE_DELETE, ids);
            if (response?.code == 1)
            {
                return true;
            }
            return false;
        }

        private void OnEdit(object obj)
        {
            if (obj is RoleDto role)
            {
                DialogParameters para = new DialogParameters();
                para.Add("data", role.DeepClone());
                _dialogService.ShowDialog(nameof(AddEditRole), para, AddEditCallBack);
            }
        }

        private void OnPrint()
        {

        }

        private void OnRefresh()
        {

        }

        private void OnGrant()
        {
            var count = RoleList.Count(p => p.IsChecked == true);
            if (count == 0)
            {
                ToastManager.ShowToast("请勾选要删除的角色");
                return;
            }
            if (count > 1)
            {
                ToastManager.ShowToast("请勾选一个角色进行权限设置");
                return;
            }
            DialogParameters para = new DialogParameters();
            var roleId = RoleList.FirstOrDefault(x => x.IsChecked == true).Id;
            para.Add("roleId", roleId);
            _dialogService.ShowDialog(nameof(MenuAuthorize), para, OnGrantCallBack);
        }

        private void OnGrantCallBack(IDialogResult result)
        {

        }

        private ICommand _addCommand;
        public ICommand AddCommand => _addCommand ??= new DelegateCommand(OnAdd);

        private ICommand _editCommand;
        public ICommand EditCommand => _editCommand ??= new DelegateCommand<object>(OnEdit);

        private ICommand _deleteCommand;
        public ICommand DeleteCommand => _deleteCommand ??= new DelegateCommand(OnDelete);

        private ICommand _printCommand;
        public ICommand PrintCommand => _printCommand ??= new DelegateCommand(OnPrint);

        private ICommand _refreshCommand;
        public ICommand RefreshCommand => _refreshCommand ??= new DelegateCommand(OnRefresh);

        private ICommand _grantCommand;
        public ICommand GrantCommand => _grantCommand ??= new DelegateCommand(OnGrant);

        private void AddEditCallBack(IDialogResult dialogResult)
        {
            if (dialogResult != null && dialogResult.Result == ButtonResult.OK)
            {
                LoadRoleList();
            }
        }
    }
}
